﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using CefSharp.OffScreen;
using CefSharp;
namespace Chen.CefWebKit
{
    class NewClient : WebClient
    {
        private Func<WebException, bool> OnWebException { get; set; }
        private Action OnWebResponseOK { get; set; }
        const int SleepTime = 100;
        public UserCookie uc;
        SqliteHelper sqlite;
        public NewClient(string CookiePath)
        {
            sqlite = new SqliteHelper(CookiePath);
            uc = new UserCookie(sqlite);
        }
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.CookieContainer = uc.cookie;
            }
            return request;
        }
        public async Task<string> PostData(string url, string postString)
        {
            try
            {
                this.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                string data = this.UploadString(url, postString);
                if (OnWebResponseOK != null)
                {
                    OnWebResponseOK();
                }
                return data;
            }
            catch (WebException ex)
            {
                if (OnWebException != null)
                {
                    OnWebException(ex);
                }
                System.Threading.Thread.Sleep(SleepTime);
            }
            return await PostData(url, postString);
        }
        public async Task<Byte[]> PostDataByte(string url, string postString)
        {
            byte[] postbyte = System.Text.Encoding.Default.GetBytes(postString);
            try
            {
                this.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                byte[] data = this.UploadData(url, postbyte);
                if (OnWebResponseOK != null)
                {
                    OnWebResponseOK();
                }
                return data;
            }
            catch (WebException ex)
            {
                if (OnWebException != null)
                {
                    OnWebException(ex);
                }
                System.Threading.Thread.Sleep(SleepTime);
            }
            return await PostDataByte(url, postString);
        }
        public async Task<byte[]> Download(string url)
        {
            try
            {
                var data = this.DownloadData(new Uri(url));
                if (OnWebResponseOK != null)
                {
                    OnWebResponseOK();
                }
                return data;
            }
            catch (WebException ex)
            {
                if (OnWebException != null)
                {
                    OnWebException(ex);
                }
                System.Threading.Thread.Sleep(SleepTime);
            }
            return await this.Download(url);
        }
        public async Task<string> DownloadHtmlCode(string url)
        {
            try
            {
                var data = this.DownloadString(new Uri(url));
                if (OnWebResponseOK != null)
                {
                    OnWebResponseOK();
                }
                return data;
            }
            catch (WebException ex)
            {
                if (OnWebException != null)
                {
                    OnWebException(ex);
                }
                System.Threading.Thread.Sleep(SleepTime);
            }
            return await this.DownloadHtmlCode(url);
        }
    }
}
